/* This file is part of SurfBoard.

   SurfBoard is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   SurfBoard is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with SurfBoard.  If not, see <http://www.gnu.org/licenses/>. */

#import "SBAboutProtocol.h"
#import "SBAppDelegate.h"
#import "SBSiteController.h"

@implementation SBAppDelegate
@synthesize latestDirectory;
@synthesize sites;
- (id)init {
    self = [super init];
    if (self) {
        latestDirectory = [NSHomeDirectory() retain];
        sites = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return self;
}
- (void)dealloc {
    [latestDirectory release];
    [sites release];
    [super dealloc];
}
- (BOOL)application:(NSApplication *)application openFile:(NSString *)file {
    [sites addObject:[[[SBSiteController alloc] initWithURL:[NSURL fileURLWithPath:file]] autorelease]];
    return YES;
}
- (void)application:(NSApplication *)application openFiles:(NSArray *)files {
    for (NSString *file in files)
        [sites addObject:[[[SBSiteController alloc] initWithURL:[NSURL fileURLWithPath:file]] autorelease]];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
    return YES;
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:
                     [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];
    // Show a window
    SBSiteController *newSite = [[[SBSiteController alloc] init] autorelease];
    // [[SBSiteController alloc] init] returned nil. Freak out.
    if (!newSite) {
        NSLog(@"**** [[SBSiteController alloc] init] returned nil. Quitting. ****");
        exit(EXIT_FAILURE);
    }
    [sites addObject:newSite];
}
- (IBAction)newWindow:(id)sender {
    [sites addObject:[[[SBSiteController alloc] init] autorelease]];
}
- (void)openPanelEnded:(NSOpenPanel *)panel code:(int)code context:(void *)info {
    if (code != NSOKButton)
        return;
    for (NSURL *file in [panel URLs])
        [sites addObject:[[[SBSiteController alloc] initWithURL:file] autorelease]];
    latestDirectory = [panel directory];
    [panel release];
}
- (IBAction)showOpenFileDialog:(id)sender {
    // Display an open panel for either the most recently selected directory or the Home directory, if there isn't one.
    [[[NSOpenPanel alloc] init] beginForDirectory:latestDirectory file:nil types:[NSArray arrayWithObjects:@"html",
                                @"htm", @"xhtml", @"xhtm", @"webarchive", nil] modelessDelegate:self
                                   didEndSelector:@selector(openPanelEnded:code:context:) contextInfo:NULL];
}
@end